home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter6 / delcol.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  8KB  |  260 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "delcol.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "ListView_DeleteColumn()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106. // Return TRUE if a column exists.
  107. //................................
  108. BOOL ColumnExists( HWND hList, int nCol )
  109. {
  110.    LV_COLUMN lvc;
  111.  
  112.    // Retrieve the width of a column to determine if it exists.
  113.    //..........................................................
  114.    lvc.mask = LVCF_WIDTH;
  115.  
  116.    return( ListView_GetColumn( hList, nCol, &lvc ) );
  117. }
  118.  
  119. // Determine the number of columns in the list view.
  120. //..................................................
  121. int GetNumColumns( HWND hList )
  122. {
  123.    int nNum = 0;
  124.  
  125.    while( ColumnExists( hList, nNum ) )
  126.       nNum++;
  127.  
  128.    return( nNum );
  129. }
  130.  
  131.  
  132. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  133. {
  134. static HWND hList = NULL;
  135.  
  136.    switch( uMsg )
  137.    {
  138.       case WM_CREATE :
  139.               {
  140.                  InitCommonControls();
  141.  
  142.                  // Create the list view.
  143.                  //......................
  144.                  hList  = CreateWindowEx( 0, WC_LISTVIEW, "",
  145.                                           WS_CHILD | WS_VISIBLE | LVS_REPORT,
  146.                                           10, 100, 100, 100,
  147.                                           hWnd,
  148.                                           (HMENU)1,
  149.                                           hInst,
  150.                                           NULL);
  151.               }
  152.               break;
  153.  
  154.       case WM_SIZE :
  155.               if ( hList )
  156.                  MoveWindow( hList, 0, 0, LOWORD( lParam ), HIWORD( lParam ), FALSE );
  157.  
  158.               if ( wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED )
  159.                  ListView_RedrawItems( hList, 0, ListView_GetItemCount( hList ) );
  160.               break;
  161.  
  162.       case WM_COMMAND :
  163.               switch( LOWORD( wParam ) )
  164.               {
  165.                  case IDM_NEWCOLUMN :
  166.                         {
  167.                            LV_COLUMN col;
  168.                            char      szName[40];
  169.                            int       nNumCols = GetNumColumns( hList );
  170.  
  171.                            // Add a new column to the list view.
  172.                            //...................................
  173.                            wsprintf( szName, "Column %d", nNumCols+1 );
  174.  
  175.                            col.mask     = LVCF_FMT | LVCF_SUBITEM | 
  176.                                           LVCF_WIDTH | LVCF_TEXT;
  177.                            col.fmt      = LVCFMT_LEFT;
  178.                            col.cx       = 100;
  179.                            col.pszText  = szName;
  180.                            col.iSubItem = nNumCols;
  181.  
  182.                            ListView_InsertColumn( hList, nNumCols, &col );
  183.                         }
  184.                         break;
  185.  
  186.                  case IDM_RENCOL :
  187.                  case IDM_RENFIELD :
  188.                         {
  189.                            LV_COLUMN col;
  190.                            int       nCol = 0;
  191.                            char      szName[40];
  192.  
  193.                            while ( ColumnExists( hList, nCol ) )
  194.                            {
  195.                               col.mask    = LVCF_TEXT;
  196.                               col.pszText = szName;
  197.                               
  198.                               if ( LOWORD( wParam ) == IDM_RENCOL )
  199.                                  wsprintf( szName, "Column %d", nCol+1 );
  200.                               else
  201.                                  wsprintf( szName, "Field %d", nCol+1 );
  202.  
  203.                               ListView_SetColumn( hList, nCol, &col );
  204.  
  205.                               nCol++;
  206.                            }
  207.                         }
  208.                         break;
  209.  
  210.                  case IDM_DELETE :
  211.                         // Delete the last column from the list view.
  212.                         //...........................................
  213.                         ListView_DeleteColumn( hList, GetNumColumns( hList )-1 );
  214.                         break;
  215.  
  216.                  case IDM_ABOUT :
  217.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  218.                         break;
  219.  
  220.                  case IDM_EXIT :
  221.                         DestroyWindow( hWnd );
  222.                         break;
  223.               }
  224.               break;
  225.       
  226.       case WM_DESTROY :
  227.               PostQuitMessage(0);
  228.               break;
  229.  
  230.       default :
  231.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  232.    }
  233.  
  234.    return( 0L );
  235. }
  236.  
  237.  
  238. LRESULT CALLBACK About( HWND hDlg,           
  239.                         UINT message,        
  240.                         WPARAM wParam,       
  241.                         LPARAM lParam)
  242. {
  243.    switch (message) 
  244.    {
  245.        case WM_INITDIALOG: 
  246.                return (TRUE);
  247.  
  248.        case WM_COMMAND:                              
  249.                if (   LOWORD(wParam) == IDOK         
  250.                    || LOWORD(wParam) == IDCANCEL)    
  251.                {
  252.                        EndDialog(hDlg, TRUE);        
  253.                        return (TRUE);
  254.                }
  255.                break;
  256.    }
  257.  
  258.    return (FALSE); 
  259. }
  260.